<?php
/**
 * Created by PhpStorm.
 * User: fengzi
 * Date: 2019/7/23
 * Time: 15:23
 */
namespace app\vehicle\logic;
use app\vehicle\model\WeixinTpl;
use app\vehicle\logic\tokenLogic;
class TplLogic {
    //签到成功
    public function signTpl($openid,$number){
        $tpl_id = '6msMkqTE-V_y0u3A9m1yN-PuvimShtHkmznR-omTlEQ';
        $access_token = tokenLogic::getAccessToken();
        $url='https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token='.$access_token;
        $data = [
            'number5'=>[
                'value'=> $number,
            ],
            'date6'=>[
                'value'=> date('Y-m-d H:i:s'),
            ]
        ];
        $template_data=[
            'touser'=>$openid,
            'template_id'=>$tpl_id,
            'page'=>'/pages/integral/info',
            'data'=>$data
        ];
        $data = json_encode($template_data);
        $a =  self::curlPost($url,$data);
    }
    //兑换成功
    public function exchangeTpl($openid,$title,$integral){
        $tpl_id = 'nFgvvm44GSJJwFzX9yy3tm20o0dS9sC46DpO8CUWqOY';
        $access_token = tokenLogic::getAccessToken();
        $url='https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token='.$access_token;
        $data = [
            'thing1'=>[
                'value'=> $title,
            ],
            'number3'=>[
                'value'=> $integral,
            ],
            'time4'=>[
                'value'=> date('Y-m-d H:i:s'),
            ]
        ];
        $template_data=[
            'touser'=>$openid,
            'template_id'=>$tpl_id,
            'page'=>'/pages/integral/index?tab=1',
            'data'=>$data
        ];
        $data = json_encode($template_data);
        $a =  self::curlPost($url,$data);
    }
    public function examineTpl($openid,$title,$val){
        $tpl_id = 'JSHtFZ-wliAVroCX78D_C4JnTlDcpWakvzL9Du7VHSA';
        $access_token = tokenLogic::getAccessToken();
        // arr($access_token);die;
        $url='https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token='.$access_token;
        $data = [
            'thing1'=>[
                'value'=> $title,
            ],
            'date3'=>[
                'value'=> date('Y-m-d H:i:s'),
            ],
            'phrase4'=>[
                'value'=> $val,
            ],
        ];
        $template_data=[
            'touser'=>$openid,
            'template_id'=>$tpl_id,
            'page'=>'/pages/user/user',
            'data'=>$data
        ];
        $data = json_encode($template_data);
        $a =  self::curlPost($url,$data);
    }
    public function examineTpls($openid,$title,$val){
        $tpl_id = 'JSHtFZ-wliAVroCX78D_C4JnTlDcpWakvzL9Du7VHSA';
        $access_token = tokenLogic::getAccessToken();
        $url='https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token='.$access_token;
        $data = [
            'thing1'=>[
                'value'=> $title,
            ],
            'date3'=>[
                'value'=> date('Y-m-d H:i:s'),
            ],
            'phrase4'=>[
                'value'=> $val,
            ],
        ];
        $template_data=[
            'touser'=>$openid,
            'template_id'=>$tpl_id,
            'page'=>'/pages/user/user',
            'data'=>$data
        ];
        $data = json_encode($template_data);
        $a =  self::curlPost($url,$data);
    }
    public function endActivityTpl($openid,$title,$id){
        $tpl_id = 'SaWMdwphiKBlp9OOzu5VhFXzSXChr7RUpKaz_Ndde84';
        $access_token = tokenLogic::getAccessToken();
        $url='https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token='.$access_token;
        $data = [
            'thing2'=>[
                'value'=> $title,
            ],
            'date1'=>[
                'value'=> date('Y-m-d H:i:s'),
            ],
            'thing4'=>[
                'value'=> '活动已结束，请尽快上传资料汇报。',
            ],
        ];
        $template_data=[
            'touser'=>$openid,
            'template_id'=>$tpl_id,
            'page'=>'/pages/wo_fabu/report?id='.$id,
            'data'=>$data
        ];
        $data = json_encode($template_data);
        $a =  self::curlPost($url,$data);
    }
    //报名
    public function applyTpl($openid,$title,$thing1,$date4,$thing5){
        $tpl_id = 'GaUJNtFC00UNQM219OVC9TXPIkfMg9cwqmYSlXcHGfc';
        $access_token = tokenLogic::getAccessToken();
        $url='https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token='.$access_token;
        $data = [
            'thing3'=>[
                'value'=> $title,
            ],
            'thing1'=>[
                'value'=> $thing1,
            ],
            'date4'=>[
                'value'=> $date4,
            ],
            'thing5'=>[
                'value'=> $thing5,
            ],
        ];
        $template_data=[
            'touser'=>$openid,
            'template_id'=>$tpl_id,
            'page'=>'/pages/fuwu/fuwu',
            'data'=>$data
        ];
        $data = json_encode($template_data);
        $a =  self::curlPost($url,$data);
        return $a;
    }
    public function examineStatus($openid,$title,$content){
        $tpl_id = 'WuNLvvteW2O8Lk03Dy6C0EEffZF-3PvFswpo25V0_00';
        $access_token = tokenLogic::getAccessToken();
        $url='https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token='.$access_token;
        $data = [
            'thing1'=>[
                'value'=> '发布活动',
            ],
            'phrase2'=>[
                'value'=> $title,
            ],
            'date3'=>[
                'value'=> date('Y-m-d H:i:s'),
            ],
            'thing4'=>[
                'value'=> $content,
            ],
        ];
        $template_data=[
            'touser'=>$openid,
            'template_id'=>$tpl_id,
            'page'=>'/pages/fuwu/fuwu',
            'data'=>$data
        ];
        $data = json_encode($template_data);
        $a =  self::curlPost($url,$data);
        return $a;
    }

    public static function curlPost($url = '', $postData = '', $options = array())
    {
        if (is_array($postData)) {
            $postData = http_build_query($postData);
        }
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
        curl_setopt($ch, CURLOPT_TIMEOUT, 30); //设置cURL允许执行的最长秒数
        if (!empty($options)) {
            curl_setopt_array($ch, $options);
        }
        //https请求 不验证证书和host
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        $data = curl_exec($ch);
        curl_close($ch);
        return $data;
    }
}